package cn.tannn.ops.nginx.controller.dto;

import cn.tannn.ops.nginx.entity.Domains;
import cn.tannn.ops.util.NginxUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.*;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

/**
 * 注册 domain
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/22 下午4:27
 */
@Schema(description = "注册domain")
@ToString
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DomainRegister {

    @Schema(description = "域名", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotBlank
    String domain;

    @Schema(description = "ssl证书路径，用于自动更新证书")
    String sslPath;

    @Schema(description = "CloudAccessKey表里的accessId")
    String accessId;

    @Schema(description = "Webhook")
    Long webhookId;

    @Schema(description = "ssh")
    Long sshId;

    public Domains toDomains() throws NoSuchAlgorithmException, IOException, KeyManagementException {
        Domains domains = NginxUtil.domainSslInfo(domain.trim());
        domains.setSslPath(sslPath);
        domains.setAccessId(accessId);
        domains.setSshId(sshId);
        domains.setWebhookId(webhookId);
        return domains;
    }
}
